﻿using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThongTinNhanKhau.GUI
{
    public partial class HoKhau : Form
    {
        model.BO.HoKhauBO hoKhauBO;
        public HoKhau()
        {
            InitializeComponent();
        }

        private void HoKhau_Load(object sender, EventArgs e)
        {
            this.gridControlHoKhau.Location = new System.Drawing.Point(3, 59);
            this.gridControlHoKhau.Size = new System.Drawing.Size(1003, 254);
            this.panelNewHoKhau.Visible = false;
            this.panelFindHoKhau.Visible = true;

            hoKhauBO = new model.BO.HoKhauBO(this);

            hoKhauBO.loadHoKhau();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            hoKhauBO.btnTimKiem();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            this.tbMaHoKhau.Text = "";
            this.tbTenChuHo.Text = "";
        }

        private void gridViewHoKhau_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                this.contextMenuStripHoKhau.Show(view.GridControl, e.Point);
            }
            else
            {
                this.contextMenuStripHoKhau.Show(view.GridControl, e.Point);
            }
        }

        private void thêmMớiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gridControlHoKhau.Location = new System.Drawing.Point(3, 99);
            this.gridControlHoKhau.Size = new System.Drawing.Size(1003, 214);
            this.panelNewHoKhau.Visible = true;
            this.panelFindHoKhau.Visible = false;

            hoKhauBO.generateMaHK();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.gridControlHoKhau.Location = new System.Drawing.Point(3, 59);
            this.gridControlHoKhau.Size = new System.Drawing.Size(1003, 254);
            this.panelNewHoKhau.Visible = false;
            this.panelFindHoKhau.Visible = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            hoKhauBO.btnLuu();
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewHoKhau.SelectedRowsCount > 0)
            {
                this.gridControlHoKhau.Location = new System.Drawing.Point(3, 99);
                this.gridControlHoKhau.Size = new System.Drawing.Size(1003, 214);
                this.panelNewHoKhau.Visible = true;
                this.panelFindHoKhau.Visible = false;

                this.tbNewMaHoKhau.Text = gridViewHoKhau.GetRowCellValue(gridViewHoKhau.FocusedRowHandle, "MaHoKhau").ToString();
                this.tbNewTenChuHo.Text = gridViewHoKhau.GetRowCellValue(gridViewHoKhau.FocusedRowHandle, "TenChuHo").ToString();
                this.dateTimeNewNgayCap.Text = gridViewHoKhau.GetRowCellValue(gridViewHoKhau.FocusedRowHandle, "NgayCap").ToString();
            }
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewHoKhau.SelectedRowsCount > 0)
            {
                String maHoKhau = this.gridViewHoKhau.GetRowCellValue(this.gridViewHoKhau.FocusedRowHandle, "MaHoKhau").ToString();
                DialogResult result = MessageBox.Show("Bạn có muốn xóa sổ hộ khẩu: " + maHoKhau + "???", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                //If Yes
                if (result == DialogResult.Yes)
                {
                    hoKhauBO.deleteHoKhau(maHoKhau);
                }

            }
        }

        private void gridViewHoKhau_RowClick(object sender, RowClickEventArgs e)
        {
            String maHoKhau = this.gridViewHoKhau.GetRowCellValue(this.gridViewHoKhau.FocusedRowHandle, "MaHoKhau").ToString();
            loadNhanKhau(maHoKhau);
        }

        public void loadNhanKhau(String maHoKhau)
        {
            hoKhauBO.loadNhanKhau(maHoKhau);
        }

        private void gridViewNhanKhau_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                this.contextMenuStripNhanKhau.Show(view.GridControl, e.Point);
            }
            else
            {
                this.contextMenuStripNhanKhau.Show(view.GridControl, e.Point);
            }
        }

        private void toolStripMenuItemThemMoiNhanKhau_Click(object sender, EventArgs e)
        {
            String maHoKhau = this.gridViewHoKhau.GetRowCellValue(this.gridViewHoKhau.FocusedRowHandle, "MaHoKhau").ToString();
            String maNhanKhau = "";
            GUI.TMNhanKhau tmNhanKhau = new TMNhanKhau(this,maHoKhau, maNhanKhau);
            tmNhanKhau.Show();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                String maHoKhau = this.gridViewHoKhau.GetRowCellValue(this.gridViewHoKhau.FocusedRowHandle, "MaHoKhau").ToString();
                String maNhanKhau = this.gridViewNhanKhau.GetRowCellValue(this.gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();
                DialogResult result = MessageBox.Show("Bạn có muốn xóa sổ nhân khẩu: " + maNhanKhau + " ra khỏi sổ hộ khẩu: "+maHoKhau, "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                //If Yes
                if (result == DialogResult.Yes)
                {
                    hoKhauBO.deleteNhanKhau(maHoKhau,maNhanKhau);
                }

            }
        }

        private void táchHộKhẩuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                String maHoKhau = this.gridViewHoKhau.GetRowCellValue(this.gridViewHoKhau.FocusedRowHandle, "MaHoKhau").ToString();
                String maNhanKhau = this.gridViewNhanKhau.GetRowCellValue(this.gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();

                GUI.TachHoKhau tachHoKhau = new TachHoKhau(this, maHoKhau, maNhanKhau);
                tachHoKhau.Show();

            }
        }
    }
}
